<?php

namespace app\admin\model\game;

use think\Model;


class Record extends Model
{

    

    

    // 表名
    protected $name = 'game_prizerecord';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];
    

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function prize()
    {
        return $this->belongsTo('Prize', 'prize_id', 'prizeId', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('User', 'openid', 'openid', [], 'LEFT')->setEagerlyType(0);
    }


    public function admin()
    {
        return $this->belongsTo('Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getPrizes($openid)
    {
        $where = ["openid" => $openid];
        $res = $this->where($where)->with(["prize"])->find();
        // 抓取prize表中的数据 prizeId name frameIndex
        // 避免name字段重复
        return $res;
    }

    public function getPrizeById($id)
    {
        $where = ["id" => $id];
        $res = $this->where($where)->find();
        return $res;
    }

    public function addData($data){
        $now = time();
        $data["createtime"] = $now;
        $data["updatetime"] = $now;
        $res = $this->insertGetId($data);
        return $res;
    }

    public function updateUserInfo($where,$data)
    {
       $res =  $this->where($where)->update($data);
       return $res;
    }

    public function getExchangeDatas($admin_id)
    {
        $where = ["admin_id" => $admin_id];
        $res = $this->where($where)->with(["user","prize"])->select(    );
        return $res;
    }
}
